/** AggregatedSample
 */

#include "data.h"

module AggregatedSampleP {
	provides interface AggregatedSample;
}

implementation {
	
	DataEntry aggregated;
	uint8_t number_of_received_samples;
	
	/** Reset all the variables. */
	void reset_aggregated_sample() {
		
		number_of_received_samples = 0;
		
		aggregated.voltage = 0;
		aggregated.temperature = 0;
		aggregated.humidity = 0;
		aggregated.lightPar = 0;
		aggregated.lightTsr = 0;      
		
	}
	
	/** Add entry to the aggregated samples. */
	command void AggregatedSample.update(DataEntry *entry) {
		
		aggregated.voltage += entry->voltage;
		aggregated.temperature += entry->temperature;
		aggregated.humidity += entry->humidity;
		aggregated.lightPar += entry->lightPar;
		aggregated.lightTsr += entry->lightTsr;
		
		++ number_of_received_samples;
		
	}
	
	/** Get the average of all collected samples.
	 * \return average, passed by value!
	 */
	command DataEntry AggregatedSample.get_aggregated_sample() {
		
		DataEntry tmp = aggregated;
		
		tmp.voltage /= number_of_received_samples;
		tmp.temperature /= number_of_received_samples;
		tmp.humidity /= number_of_received_samples;
		tmp.lightPar /= number_of_received_samples;
		tmp.lightTsr /= number_of_received_samples;
		
		reset_aggregated_sample();
		return tmp;
		
	}
	
  /** Get the sample counter.
  * \return sample counter
  */
	command uint8_t AggregatedSample.get_sample_count() {
		return number_of_received_samples;
	}
	
}